/* ************************************************************************
> File Name:     designList.cpp
> Author:        程序员lyl
> 微信公众号:    雨特宙
> Created Time:  2024年04月05日 星期五 09时26分20秒
> Description:   
 ************************************************************************/

class MylinkedList{
public:
    struct LinkedNode{
        int val;
        LinkedNode* next;
        LinkedNode(int val):val(val),next(nullptr){}
    };

    MylinkedList(){
        _dummyHead = new LinkedNode(0);
        _size = 0;

    }
//获取第index个节点数值
    int get(int index){

        if(index > (_size - 1) || index < 0){
            return -1;
        }
        LinkedNode* cur = _dummyHead->next;
        while(index--){
            cur = cur->next;
        }
        return cur->val;
    }

void addAtHead(int index){
    LinkedNode* newNode = new LinkedNode(val);
    newNode->next = _dummyHead->next;
    _dummyHea->next = newNode;
    _size++;
}

void addAtTail(int val){
    LinkedNode* newNode = new LinkedNode(val);
    LinkedNode* cur = _dummyHead;
    while(cur->next){
        cur = cur->next;
    }
    cur->next = newNode;
    _size++;
    
}

void deleteAtIndex(int index){

    if(index >= _size || index < 0)
        return;
    LinkedNode* cur = _dummyHead;
    while(index--){
        cur = cur->next;
    }
    LinkedNode* tmp = cur->next;
    cur->next = cur->next->next;
    delete tmp;
    tmp = nullptr;
    _size--;

}

void printLinkedList(){
    LinkedNode* cur = _dummyHead;
    while(cur->next != nullptr){
        cout<<cur->next->val;
        cur = cur->next;
    }
    cout << endl;
}

private:
    int _size;
    LinkedNode* _dummyHead;
};
